package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dtos.ReadBehaviorDto;
import com.heima.behavior.pojos.ApReadBehavior;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 用户阅读行为
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //校验参数
        if (dto.getArticleId() == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取登录用户id
        ApUser apUser = ApThreadLocalUtil.getUser();
        //表示未登录
        if(apUser==null){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //游客身份不能进行以下操作
        if(apUser.getId()==0){
            return ResponseResult.okResult(405,"请先登录");
        }
        //查询mongodb是否有数据,有就更新,没有就新增
        ApReadBehavior apReadBehavior = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(apUser.getId())
                .and("articleId").is(dto.getArticleId())), ApReadBehavior.class);
        if(apReadBehavior != null){
            //更新次数和时间
            apReadBehavior.setCount(apReadBehavior.getCount() + dto.getCount());
            apReadBehavior.setUpdatedTime(new Date());
        }else{
            apReadBehavior = new ApReadBehavior();
            BeanUtils.copyProperties(dto,apReadBehavior);
            apReadBehavior.setCreatedTime(new Date());
            apReadBehavior.setUpdatedTime(new Date());
            apReadBehavior.setUserId(apUser.getId().longValue());
            apReadBehavior.setId(ObjectId.get().toHexString());
            apReadBehavior.setCount(1);
        }
        mongoTemplate.save(apReadBehavior);

        //实时更新阅读数据
        UpdateArticleMess articleMess = new UpdateArticleMess();
        articleMess.setArticleId(dto.getArticleId());
        articleMess.setAdd(1);
        articleMess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        //发送消息
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(articleMess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
